package com.kafka.kafkademo.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @description
 * @author: yx-0173
 * @date: 2021-06-02 16:52
 **/
@Component
@Slf4j
public class KafkaConsumer {


    @KafkaListener(topics = "yzr", groupId = "A")
    public void topicTest(ConsumerRecord<?, ?> record, Acknowledgment ack) {
        Optional<?> message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info("topic_test 消费了： Topic:"  + ",Message:" + msg);
            ack.acknowledge();
        }
    }
    @KafkaListener(topics = "yzr", groupId = "B")
    public void topicTest2(ConsumerRecord<?, ?> record, Acknowledgment ack) {
        Optional<?> message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info("topic_test2 消费了： Topic:"  + ",Message:" + msg);
            ack.acknowledge();
        }
    }
}
